package org.za.communication.test;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.za.communication.core.codec.decoder.DataSegmentationDecoder;
import org.za.communication.core.codec.encoder.DataCombineEncoder;
import org.za.communication.core.handler.SessionIdleHandler;
import org.za.communication.message.GetVerifyCodeMessage;

public class MyClient {
	public static void main(String args[]) throws Exception {

		byte[] head = new byte[2];
		head[0] = (byte) 0xff;
		head[1] = (byte) 0xff;

		List<Integer> l = new ArrayList<Integer>();//需要加密
		l.add(0x01);
		l.add(0x02);
		l.add(0x03);
		l.add(0x05);
		l.add(0x06);
		List<Integer> l2 = new ArrayList<Integer>();//需要解密
		l2.add(0x04);
		l2.add(0x52);
		l2.add(0x53);
		l2.add(0x54);

		DataCombineEncoder encoder = new DataCombineEncoder();
		encoder.setHead(head);
		encoder.setEncryptIdList(l);

		DataSegmentationDecoder decoder = new DataSegmentationDecoder();
		decoder.setHead(head);
		decoder.setDecryptIdList(l2);

		IoConnector connector = new NioSocketConnector();
		connector.setConnectTimeoutMillis(30000);

		connector.getFilterChain().addLast("mycodec",
				new ProtocolCodecFilter(encoder, decoder));
		connector.setHandler(new SessionIdleHandler());
		ConnectFuture future = connector.connect(new InetSocketAddress(
				"tsp.navi168.com", 9123));
		future.awaitUninterruptibly();
		IoSession session = future.getSession();

		GetVerifyCodeMessage message = new GetVerifyCodeMessage();

		try {
			session.write(message);

		} catch (Exception ee) {
			System.out.println(ee);
		}

	}
}
